package cn.stylefeng.guns.modular.cb.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.model.params.CbCustomerCompanyParam;
import cn.stylefeng.guns.modular.cb.service.*;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 统计
 */
@Controller
@RequestMapping("/api/count")
public class CountController {

    private static Logger logger = LoggerFactory.getLogger(CountController.class);

    @Autowired
    private CbCustomerCompanyService companyService;

    @Autowired
    private CbCustomerStaffService staffService;

    @Autowired
    private CbCustomerStudentsService studentsService;

    @Autowired
    private CbPositionService positionService;

    @Autowired
    private CbResumeDeliveryService resumeDeliveryService;

    @Autowired
    private CbResumeService resumeService;

    @Autowired
    private CbWxUserService wxUserService;

    private String PREFIX = "/cb/count";

    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2021-02-06
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/addUserCount.html";
    }

    /**
     * 分享页面
     *
     * @author
     * @Date 2021-02-06
     */
    @RequestMapping("/referees")
    public String referees() {
        return PREFIX + "/referees.html";
    }

    /**
     * 新增用户
     * @return
     */
    @RequestMapping("/add_user_count")
    @ResponseBody
    public ResponseData addUserCount(String startDate, String endDate) {
        int num = calcBetweenDate(startDate, endDate);
        String[] countArr = new String[num];

        List<Map<String, Object>> companyCount = companyService.queryDateCount(startDate, countArr);//每日企业新增数量
        List<Map<String, Object>> staffCount = staffService.queryDateCount(startDate, countArr);   //每日员工新增数量
        List<Map<String, Object>> studentCount = studentsService.queryDateCount(startDate, countArr);    //每日普通用户新增数量
        List<Map<String, Object>> positionCount = positionService.queryDateCount(startDate, countArr);  //每日新增职位
        List<Map<String, Object>> resumeCount = resumeService.queryDateCount(startDate, countArr);  //每日简历新增
        List<Map<String, Object>> resumeDeliveryCount = resumeDeliveryService.queryDateCount(startDate, countArr);//每日新增投递

        Map<String, Integer> companyMap = new LinkedHashMap<>();
        for (int i = 0; i < companyCount.size(); i++) {
            Map<String, Object> map = companyCount.get(i);
            companyMap.put(map.get("date").toString(), Integer.valueOf(map.get("count").toString()));
        }

        Map<String, Integer> staffMap = new LinkedHashMap<>();
        for (int i = 0; i < staffCount.size(); i++) {
            Map<String, Object> map = staffCount.get(i);
            staffMap.put(map.get("date").toString(), Integer.valueOf(map.get("count").toString()));
        }

        Map<String, Integer> studentMap = new LinkedHashMap<>();
        for (int i = 0; i < studentCount.size(); i++) {
            Map<String, Object> map = studentCount.get(i);
            studentMap.put(map.get("date").toString(), Integer.valueOf(map.get("count").toString()));
        }

        Map<String, Integer> positionMap = new LinkedHashMap<>();
        for (int i = 0; i < positionCount.size(); i++) {
            Map<String, Object> map = positionCount.get(i);
            positionMap.put(map.get("date").toString(), Integer.valueOf(map.get("count").toString()));
        }

        Map<String, Integer> resumeMap = new LinkedHashMap<>();
        for (int i = 0; i < resumeCount.size(); i++) {
            Map<String, Object> map = resumeCount.get(i);
            resumeMap.put(map.get("date").toString(), Integer.valueOf(map.get("count").toString()));
        }

        Map<String, Integer> resumeDeliveryMap = new LinkedHashMap<>();
        for (int i = 0; i < resumeDeliveryCount.size(); i++) {
            Map<String, Object> map = resumeDeliveryCount.get(i);
            resumeDeliveryMap.put(map.get("date").toString(), Integer.valueOf(map.get("count").toString()));
        }

        Map<String, Map<String, Integer>> map = new HashMap<>();
        map.put("companyCount", companyMap);
        map.put("staffCount", staffMap);
        map.put("studentCount", studentMap);

        map.put("positionMap", positionMap);
        map.put("resumeMap", resumeMap);
        map.put("resumeDeliveryMap", resumeDeliveryMap);
        return ResponseData.success(map);
    }

    /**
     * 用户日活/月活
     */
    @RequestMapping("/user_active")
    @ResponseBody
    public ResponseData userActiveCount(String startDate, String endDate){
        String[] countArr = new String[30];
        String[] mouthArr = new String[12];
        List<Map<String, Object>> userByDayCount = wxUserService.queryDateCount(startDate, countArr);//每日活跃用户
        List<Map<String, Object>> userByMouthCount = wxUserService.queryMouthCount(startDate, mouthArr);//每月活跃用户

        Map<String, Integer> userByDayMap = new LinkedHashMap<>();
        for (int i = 0; i < userByDayCount.size(); i++) {
            Map<String, Object> map = userByDayCount.get(i);
            userByDayMap.put(map.get("date").toString(), Integer.valueOf(map.get("count").toString()));
        }

        Map<String, Integer> userByMouthMap = new LinkedHashMap<>();
        for (int i = 0; i < userByMouthCount.size(); i++) {
            Map<String, Object> map = userByMouthCount.get(i);
            userByMouthMap.put(map.get("date").toString(), Integer.valueOf(map.get("count").toString()));
        }

        Map<String, Map<String, Integer>> map = new HashMap<>();
        map.put("userByDayMap", userByDayMap);
        map.put("userByMouthMap", userByMouthMap);
        return ResponseData.success(map);
    }



    public int calcBetweenDate(String start, String end) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = df.parse(start);
            endDate = df.parse(end);
        } catch (Exception e) {
            System.out.println("日期转换出错");
        }
        int count = (int) ((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
        return count;
    }

    /**
     * 查询分享人，及分享数量
     * @return
     */
    @RequestMapping("/referess_list")
    @ResponseBody
    public LayuiPageInfo referessList(CbCustomerCompanyParam param){
        return companyService.referessList(param);
    }
}